<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Tuvan extends CI_Controller
{
	public function __construct()
	{
        parent::__construct();
		$this->pre_message = "";
        $this->load->model('tuvan_model','tuvan');
	}
	
	function index($start=0)
	{
		$data['title'] 	= 'Tư vấn khách hàng';
		$data['add'] 	= 'tuvan/add';
		
		$config['base_url'] 	= base_url().'tuvan/index/'.(int)$start;  
		$data['num'] 			= $this->tuvan->count_list();
		$config['total_rows'] 	= $data['num'];
		$config['per_page'] 	= 20;
		$config['uri_segment'] 	= 4;   
		$this->pagination->initialize($config);   
		$data['pagination'] 	= $this->pagination->create_links();
		
		$data['start'] = $start ;    
		$data['list'] =   $this->tuvan->get_list($config['per_page'],(int)$start);
		$data['page'] = 'index';
		$this->load->view('layout/skin',$data);
	}
	
	function add()
	{
		$data['title'] = 'Thêm tư vấn mới';
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('name_vn','Tiêu đề','trim|required');
			$this->form_validation->set_rules('content_vn','Trả lời','trim|required');
			if($this->form_validation->run()== FALSE){
				$this->pre_message = validation_errors();
			}else{
				$req = $this->_build_data(1);
			  	if($this->tuvan->save('tuvan', $req))
			  	{
					$this->session->set_flashdata('message','Lưu thành công');
					redirect('tuvan/index');
			  	}
			}
		}
		
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'tuvan/add';
		$this->site->load($this->_templates['page'],$data);
	}

	function edit($id=0)
	{            
		$data['rs'] = $this->tuvan->getId($id);
		$data['title'] = 'Cập nhật: '.$data['rs']->name_vn;
		
		if(!empty($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('name_vn','Tên chủ đề','trim|required');
			if($req['lb_image']) $this->delimg('../uploads/tuvan/'.$data['rs']->lb_image);
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}else{
				$req = $this->_build_data();
				if($this->tuvan->save('tuvan',$req,'id',$id))
				{
					$this->session->set_flashdata('message','Cập nhật chuyên mục thành công');
					redirect('tuvan');
				}
		 	}
		}  
		$this->_templates['page'] = 'tuvan/edit';
		$this->site->load($this->_templates['page'],$data);
	}
	
	private function _build_data($flag=0)
	{
		$req["cusmail"]	= $this->input->post("cusmail");
		$req["cusname"]	= $this->input->post("cusname");
		$req["name_vn"]	= $this->input->post("name_vn");
		$req["name_en"] 	= $this->input->post("name_en");
		$req["question_vn"]	= $this->input->post("question_vn");
		$req["question_en"]	= $this->input->post("question_en");
		$req["content_vn"] 	= $this->input->post("content_vn");
		$req["content_en"] 	= $this->input->post("content_en");
		$req["nb_order"] 	= (int) $this->input->post("nb_order");
		
		$req["dt_modify"] 	= date('Y-m-d H:i:s');
		if($flag) $req["dt_create"] = date('Y-m-d H:i:s');
		$req["bl_active"] 	= (int)$this->input->post("bl_active");
		
		//upload hinh anh
		$img = $this->input->post("lb_image");
		$img_name = end(explode('/', $img)) ;
		if($img)
		{
			if(file_exists('../uploads/tuvan/'.$img_name))
			{
				unlink('../uploads/tuvan/'.$img_name);
			}
			fn_resize_image('../uploads/'.$img,'../uploads/tuvan/'.$img_name, 150, 100); 
			$req["lb_image"] = $img_name;
		}
		return $req;
	}

	private function delimg($filename='')
	{
		if(!empty($filename) && file_exists($filename))@unlink($filename);
	}
	
	function view($id=0)
	{            
		$data['rs'] = $this->tuvan->getId($id);
		$data['title'] = 'Xem thông tin liên hệ từ '.$data['rs']->lb_cusmail;
		
		//Cập nhật trang thái
		$this->tuvan->set_view($id);
		 
		$data['page'] = 'view';
		$this->load->view('layout/skin',$data);
	}
	
	function anser()
	{
		 $data['title'] = ''; 
		 $this->form_validation->set_rules('noidung_vn','','trim');
		 $this->form_validation->set_rules('noidung_en','','trim');
		 if($this->form_validation->run()== FALSE){
		  $this->pre_message = validation_errors();
	  }else{
		  $id=$this->input->post('id');
		  $noidung_vn=$this->input->post('noidung_vn');
		  $noidung_en=$this->input->post('noidung_en');
		  $cusname=$this->input->post('cusname');
		   if($this->tuvan->save($id,$noidung_vn,$noidung_en,$cusname)){
			   echo 1;die();
		   }else{
		  	 echo 2;die();
		   }
	  }
	}
	
	function editAnser()
	{
		$data['title'] = ''; 
		
		if(!empty($_POST['bt_submit'])){
			 $data['title'] = ''; 
			 $this->form_validation->set_rules('noidung_vn','','trim');
			 $this->form_validation->set_rules('noidung_en','','trim');
			 if($this->form_validation->run()== FALSE){
					  $this->pre_message = validation_errors();
			  }else{
				  $id=$this->input->post('id');
				  $noidung_vn=$this->input->post('noidung_vn');
				  $noidung_en=$this->input->post('noidung_en');
				  $cusname=$this->input->post('cusname');
				   if($this->tuvan->update($id,$noidung_vn,$noidung_en,$cusname)){
					   $this->session->set_flashdata('message','Lưu thành công');
					  redirect('tuvan/index');
				   }
			  }
		}
	  $id=$this->uri->segment(3);
	  $data['rs'] = $this->tuvan->getId($id);
	  $data['message'] = $this->pre_message;
	  $this->_templates['page'] = 'tuvan/editanser';
	  $this->site->load($this->_templates['page'],$data);
	}
	
	function del($id=0,$start=0)
	{
		$this->tuvan->delete($id);
		$this->tuvan->remove();
		redirect("tuvan/index/".$start);
	}
	
	function dels()
	{
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
		
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->tuvan->delete($ar_id[$i]))
							$this->session->set_flashdata('message','Đã xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}
		redirect('tuvan/index/'.$page);
	}
}

?>